package com.epfresh.supplier.activity.my_contract;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.MyContractRecyclerViewAdapter;
import com.epfresh.supplier.adapter.TestItemRecyclerViewAdapter;
import com.epfresh.supplier.adapter.TestReportRecyclerViewAdapter;
import com.epfresh.supplier.entity.ContractEntity;
import com.epfresh.supplier.entity.TestItemEntity;
import com.epfresh.supplier.entity.TestReport;
import com.epfresh.supplier.entity.TestReportEntity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyContractActivity extends BaseActivity {
    private RecyclerView rv_my_contract;
    private List<ContractEntity> list;
    private MyContractRecyclerViewAdapter myContractRecyclerViewAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_contract);
        tvBack.setVisibility(View.VISIBLE);
        ivBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("我的合同");

        initView();
        initData();

        //加载数据
//        requestTestReportDetail();

    }



    private void initView() {
        rv_my_contract=findViewById(R.id.rv_my_contract);
    }

    private void initData() {
        list=new ArrayList<>();
        ContractEntity contractEntity=new ContractEntity();
        contractEntity.setId("1");
        contractEntity.setContractName("仓库租赁合同");
        contractEntity.setContractStatusType(1);
        contractEntity.setContractStatus("待生效");
        contractEntity.setSignContractDate("2019.11.02");
        list.add(contractEntity);

        ContractEntity contractEntity2=new ContractEntity();
        contractEntity2.setId("2");
        contractEntity2.setContractName("摊位租金合同");
        contractEntity2.setContractStatusType(2);
        contractEntity2.setContractStatus("生效中");
        contractEntity2.setSignContractDate("2019.11.11");
        list.add(contractEntity2);

        //创建adapter
        myContractRecyclerViewAdapter=new MyContractRecyclerViewAdapter(this,list);
        //绑定数据
        myContractRecyclerViewAdapter.setOnBindData(new MyContractRecyclerViewAdapter.BindData() {
            @Override
            public void onBind(MyContractRecyclerViewAdapter.ViewHolder viewHolder, ContractEntity contractEntity, int position) {
                viewHolder.tv_contract_name.setText(contractEntity.getContractName());//合同名称
                viewHolder.tv_contract_status.setText(contractEntity.getContractStatus());//合同状态
                viewHolder.tv_sign_contract_date.setText(contractEntity.getSignContractDate());//签约时间
                switch (contractEntity.getContractStatusType()){
                    case 1://待生效
                        viewHolder.tv_contract_status.setTextColor(getResources().getColor(R.color.coloreb6d20));
                        break;
                    case 2://生效中
                        viewHolder.tv_contract_status.setTextColor(getResources().getColor(R.color.color3eb049));
                        break;
                    case 3://已终止
                        viewHolder.tv_contract_status.setTextColor(getResources().getColor(R.color.colordd4b39));
                        break;
                    case 4://已解除
                        viewHolder.tv_contract_status.setTextColor(getResources().getColor(R.color.color898989));
                        break;
                }
            }
        });
        //点击项事件
        myContractRecyclerViewAdapter.setOnItemClickListener(new MyContractRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(MyContractRecyclerViewAdapter.ViewHolder viewHolder, int position) {
                Intent intent=new Intent(MyContractActivity.this,MyContractDetailActivity.class);
                intent.putExtra("contractId",list.get(position).getId());
                startActivity(intent);
            }
        });
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        rv_my_contract.setLayoutManager(linearLayoutManager);
        rv_my_contract.setAdapter(myContractRecyclerViewAdapter);


    }



    /**
     * 加载合同列表数据
     */
    private void requestTestReportDetail() {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductIncomeForDayOfMonth);
        Map<String, String> map = new HashMap<>();
        map.put("storeId", BaseApplication.getInstance().getStoreId());

        map.put("accountId",getUser().getAccountId());
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductIncomeForDayOfMonth, onRequestListener);
    }

    private OnRequestListener<TestReport> onRequestListener = new OnRequestListener<TestReport>() {
        @Override
        public TestReport jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, TestReport.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<TestReport> response, Object tag) {
            //隐藏加载动画
            hideProgressDialog();
            //获取数据
            TestReport responseElement = response.getResponseElement();
            //显示数据
            showInfo();

        }
    };

    private void showInfo() {

    }

}
